import requests
import pymysql

# 创建一个mysql的链接对象
conn = pymysql.Connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='boboadmin',
    db='spider',
    charset='utf8'
)
# 创建游标对象
cursor = conn.cursor()

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
}
url = 'https://movie.douban.com/j/chart/top_list'
params = {
    "type": "24",
    "interval_id": "100:90",
    "action": "",
    "start": "20",
    "limit": "20"
}
movie_data = requests.get(url=url, headers=headers, params=params).json()
# 电影名字，评分，导演，上映时间
for dic in movie_data:
    title = dic['title']
    score = dic['score']
    actors = dic['actors']
    actors = ','.join(actors)
    year = dic['release_date']

    sql = 'insert into movie_tb(title,score,actors,year) values ("%s","%s","%s","%s")' % (title, score, actors, year)
    cursor.execute(sql)
    conn.commit()
    print(title, '：电影数据爬取存储成功！')

cursor.close()
conn.close()